package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.model.auto.ArchivesConfigItem;
import com.model.auto.MajorCategoryInfo;
import com.model.auto.MajorLimitInfo;
import com.service.IMajorCategoryInfoService;
import com.service.IMajorLimitInfoService;
import com.service.IStuArchivesRelService;
import com.util.R;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 学科门类信息表 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/majorCategoryInfo")
public class MajorCategoryInfoController {
    @Autowired
    private IMajorCategoryInfoService majorCategoryInfoService;
    @Autowired
    private IMajorLimitInfoService infoService;
    @Autowired
    private IStuArchivesRelService stuArchivesRelService;

    @GetMapping("/getMajor")
    public R getMajor(Integer level, Integer parentId, Integer catType) {
        if(ObjectUtils.isEmpty(catType)){
            catType = 1;
        }
        List<MajorCategoryInfo> result = majorCategoryInfoService.list(new LambdaQueryWrapper<MajorCategoryInfo>()
                .eq(level != null && !"".equals(level), MajorCategoryInfo::getLevel, level)
                .eq(parentId != null && !"".equals(parentId), MajorCategoryInfo::getParentId, parentId)
                .eq(catType != null && !"".equals(catType), MajorCategoryInfo::getCatType, catType)
                .orderByAsc(MajorCategoryInfo::getSortNum)
        );
        result.forEach(i -> {
            if (i.getLevel() == 1) {
                switch (i.getName()) {
                    case "理学":
                        i.setSortNum(1);
                        break;
                    case "工学":
                        i.setSortNum(2);
                        break;
                    case "医学":
                        i.setSortNum(3);
                        break;
                    case "农学":
                        i.setSortNum(4);
                        break;
                    case "文学":
                        i.setSortNum(5);
                        break;
                    case "历史学":
                        i.setSortNum(6);
                        break;
                    case "哲学":
                        i.setSortNum(7);
                        break;
                    case "法学":
                        i.setSortNum(8);
                        break;
                    case "经济学":
                        i.setSortNum(9);
                        break;
                    case "管理学":
                        i.setSortNum(10);
                        break;
                    case "教育学":
                        i.setSortNum(11);
                        break;
                    case "艺术学":
                        i.setSortNum(12);
                        break;
                    default:
                        i.setSortNum(13);
                        break;
                }
            } else {
                i.setSortNum(999);
                List<MajorCategoryInfo> childData = majorCategoryInfoService.list(new LambdaQueryWrapper<MajorCategoryInfo>()
                        .eq(MajorCategoryInfo::getParentId, i.getId())
                        .orderByAsc(MajorCategoryInfo::getSortNum)
                );
                i.setChildData(childData);
            }
        });
        List<MajorCategoryInfo> list = result.stream().sorted(Comparator.comparing(MajorCategoryInfo::getSortNum)).collect(Collectors.toList());
        return R.ok(list);
    }

    @GetMapping("getMajors")
    public R getMajors(Integer stuId) {
        List<MajorCategoryInfo> list = majorCategoryInfoService.list(
            new LambdaQueryWrapper<MajorCategoryInfo>()
                    .eq(MajorCategoryInfo::getCatType, 1)
                    .orderByAsc(MajorCategoryInfo::getSortNum)
        );
        ArrayList<Object> majorCodeList = new ArrayList<>();
        list.forEach(i -> majorCodeList.add(i.getCode()));
        List<MajorLimitInfo> majorLimitInfos = infoService.list(new LambdaQueryWrapper<MajorLimitInfo>()
                .in(MajorLimitInfo::getMajorCode, majorCodeList)
        );
        list.forEach(majorInfo -> {
            List<MajorLimitInfo> majorLimits = majorLimitInfos.stream().filter(data -> (data.getMajorCode().equals(majorInfo.getCode()))).collect(Collectors.toList());
            String limitDesc = majorLimits.stream().map(MajorLimitInfo::getArchivesCode).collect(Collectors.joining(", "));
            majorInfo.setLimitDesc(limitDesc);
        });
        return R.ok(list);
    }

    @GetMapping("getMajorFather")
    public R getMajor(Integer parentId) {
        MajorCategoryInfo majorCategoryInfo = majorCategoryInfoService.getOne(new LambdaQueryWrapper<MajorCategoryInfo>()
                .eq(parentId != null, MajorCategoryInfo::getId, parentId));
        return R.ok(majorCategoryInfo);
    }

    /**
     * 专业搜索
     * @param keyword
     * @return
     */
    @GetMapping("/searchMajor")
    public R searchMajor(String keyword) {
        List<MajorCategoryInfo> list = majorCategoryInfoService.list(
                new LambdaQueryWrapper<MajorCategoryInfo>()
                        .eq(MajorCategoryInfo::getCatType, 1)
                        .eq(MajorCategoryInfo::getLevel, 3)
                        .like(MajorCategoryInfo::getName, keyword)
                        .orderByAsc(MajorCategoryInfo::getSortNum)
        );
        return R.ok(list);
    }
}
